﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using GeneratorLib.DB.Schemas;
using GeneratorLib.DB.Connectios;
using MSSQLProvider.DBUtility;
using GeneratorLib.DB;

namespace MSSQLProvider.DBConnection
{
    [Serializable]
    public class SQLServerConnection : DBConnectionBase, ISerializable
    {

        private static readonly SQLServerProvider _provoder  = new SQLServerProvider();

        public override string DBTypeMapping
        {
            get
            {
                return "SQLDbType";
            }
        }
        //DBParameter------------------------------------------------------
        public override string DbParamType
        {
            get
            {
                return "SqlParameter";
            }
        }
        public override string DbParamDbType
        {
            get
            {
                return "SqlDbType";
            }
        }
        public override string PreParameter
        {
            get
            {
                return "@";
            }
        }
        //DBParameter------------------------------------------------------

        public SQLServerConnection(string name, string connectStr)
            : base(name, connectStr)
        {
        }

        public SQLServerConnection(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public override IDBProvider DBProvider
        {
            get
            {
                return _provoder;
            }
        }

        public override List<FieldInfo> GetTableField(TableInfo table)
        {
            DataSet dtSet = SQLServerUtility.GetTableFields(this.ConnectStr, table.TableName);
            List<FieldInfo> fields = new List<FieldInfo>();

            string keyField = SQLServerUtility.GetPrimaryKey(this.ConnectStr, table.TableName);

            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                FieldInfo field = new FieldInfo();
                field.Table = table;
                field.FieldName = string.Format("{0}", row["FieldName"]);
                field.SetFieldType(string.Format("{0}", row["TypeName"]));
                field.Nullabled = Convert.ToBoolean(row["Nullable"]);
                field.FieldSize1 = Convert.ToInt32(row["max_length"]);
                if (field.FieldType.TypeName.ToLower() == "nvarchar" || field.FieldType.TypeName.ToLower() == "nchar")
                {
                    field.FieldSize1 = Convert.ToInt32(row["max_length"])/2;
                }
                field.Key = (field.FieldName.CompareTo(keyField) == 0);
                fields.Add(field);
            }
            return fields;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

        protected override void FillTables()
        {
            tables = new List<TableInfo>();
            DataSet dtSet = null;
            try
            {
                dtSet = SQLServerUtility.GetTableNames(this.ConnectStr);
            }
            catch
            {
                return;
            }

            if (dtSet == null) return;

            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                TableInfo table = new TableInfo(this, string.Format("{0}", row[0]));
                tables.Add(table);
            }
        }

        protected override void FillTableAddins()
        {
            tableAddins = new List<TableInfoAddin>();
            DataSet dtSet = null;
            try
            {
                dtSet = SQLServerUtility.GetTableNames(this.ConnectStr);
            }
            catch
            {
                return;
            }

            if (dtSet == null) return;

            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                TableInfoAddin tableAddin = new TableInfoAddin(string.Format("{0}", row[0]));
                tableAddins.Add(tableAddin);
            }
        }
    }

}
